﻿using CQIE.JonJack.Models;
using CQIE.JonJack.Models.Tabels;
using CQIE.JonJack.UtilitySource;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CQIE.JonJack.MQList.IOServices
{
    public class BookService
    {
        //数据库操作对象
        private readonly _DbContext Db;
        private readonly IOptionsSnapshot<JWTSettings> jwtSettingsOpt;

        public BookService(_DbContext db, IOptionsSnapshot<JWTSettings> jwtSettingsOpt)
        {
            Db = db;
            this.jwtSettingsOpt = jwtSettingsOpt;
        }

        public async void destructBook(int bid)
        {
            //获取所有的图书
            using (_DbContext _Db = new _DbContext())
            {
                Book book = _Db.Books.Where(b => b.Id == bid).Single();
                book.isOnShell = false;
                await _Db.SaveChangesAsync();
            }
        }

        public async void setNewBook(string newBook)
        {
            // 反序列化JSON字符串为匿名对象
            var messageObject = JsonConvert.DeserializeObject<dynamic>(newBook);

            using (_DbContext _Db=new _DbContext())
            {
                Book book = new Book();
                book.BookName = messageObject.bookName;
                book.Author = messageObject.bookAuthor;
                book.ISBN = messageObject.iSBN;
                book.Pubblish = messageObject.bookPublish;
                book.Price = messageObject.bookPrice;
                book.Inventory = messageObject.bookInventory;
                book.BookClass = messageObject.bookClass;
                book.BookShelfNumber = messageObject.bookSNumber;
                book.isOnShell = messageObject.isOnshell;

                _Db.Books.Add(book);
                await _Db.SaveChangesAsync();
            }

        }
    }
}
